/* 
 * BusinessServiceImpl.java 28.03.2011
 *  
 *  
 * Candidate: Alessandra Otterbach
 * Prometric ID: Algisina1
 * Username: Algisina
 *  
 * Sun Certified Developer for Java 2 Platform, Standard Edition Programming 
 * Assignment (CX-310-252A)
 *  
 * This class is part of the Programming Assignment of the Sun Certified 
 * Developer for Java 2 Platform, Standard Edition certification program, must 
 * not be used out of this context and must be used exclusively by Sun 
 * Microsystems, Inc. 
 */
package suncertify.bs;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.List;
import java.util.Map;

import suncertify.dao.EnumSearchCriteria;
import suncertify.dao.RoomRecord;
import suncertify.db.DB;
import suncertify.db.Data;
import suncertify.db.DataException;
import suncertify.db.RecordAlreadyBookedException;
import suncertify.db.RecordNotFoundException;
import suncertify.db.SecurityException;
import suncertify.util.LoggingFacade;
import suncertify.util.RoomConverter;
import suncertify.util.StringUtils;

/**
 * Implementing the business service methods. Can be used locally or remotelly.
 * 
 * @author Alessandra Otterbach
 * 
 */
public class BusinessServiceImpl implements BusinessService {

    public DB database = null;

    private static final LoggingFacade logger = LoggingFacade
	    .getInstance(BusinessServiceImpl.class);

    public BusinessServiceImpl(String path) throws IOException {
	database = Data.getInstance(path);
    }

    /*
     * (non-Javadoc)
     * 
     * @see suncertify.bs.BusinessService#searchRoom(java.util.Map)
     */
    @Override
    public List<RoomRecord> searchRoom(
	    Map<EnumSearchCriteria, String> searchCriterias)
	    throws RecordNotFoundException, DataException, RemoteException {
	logger.info("searchRoom()", "entering method");
	SearchRoomManager searchManager = new SearchRoomManager();

	List<RoomRecord> allRooms = searchManager.searchRoom(database,
		searchCriterias);
	logger.info("searchRoom()", "exiting method");
	return allRooms;
    }

    /*
     * (non-Javadoc)
     * 
     * @see suncertify.bs.BusinessService#bookRoom(int,
     * suncertify.dao.RoomRecord)
     */
    @Override
    public void bookRoom(int recNo, RoomRecord room)
	    throws RecordNotFoundException, SecurityException, RemoteException,
	    RecordAlreadyBookedException, DataException {
	logger.info("bookRoom()", "entering method");
	long lockCookie = 0;
	try {
	    // First lock the record.
	    lockCookie = database.lock(recNo);
	    // Then make sure, it hasn't been booked from another customer
	    RoomRecord oldRoom = RoomConverter
		    .arrayAsRoom(database.read(recNo));

	    if (StringUtils.isEmpty(oldRoom.getOwner())) {
		// Then update the record
		database.update(recNo, RoomConverter.roomAsArray(room),
			lockCookie);

	    } else {
		throw new RecordAlreadyBookedException();
	    }
	} finally {
	    // Lock must always be released
	    database.unlock(recNo, lockCookie);
	}
    }
}
